<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="名称" prop="thirdPartyName">
      <el-input v-model="dataForm.thirdPartyName" placeholder="名称"></el-input>
    </el-form-item>
    <el-form-item label="归类1.机房信息2.服务器信息3.设备安全登陆信息 4.智能管理" prop="typeId">
      <el-input v-model="dataForm.typeId" placeholder="归类1.机房信息2.服务器信息3.设备安全登陆信息 4.智能管理"></el-input>
    </el-form-item>
    <el-form-item label="父类型id
" prop="parentId">
      <el-input v-model="dataForm.parentId" placeholder="父类型id
"></el-input>
    </el-form-item>
    <el-form-item label="型号" prop="modelNumber">
      <el-input v-model="dataForm.modelNumber" placeholder="型号"></el-input>
    </el-form-item>
    <el-form-item label="归属机构" prop="parentDeptId">
      <el-input v-model="dataForm.parentDeptId" placeholder="归属机构"></el-input>
    </el-form-item>
    <el-form-item label="状态0无效1有效" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态0无效1有效"></el-input>
    </el-form-item>
    <el-form-item label="所属位置" prop="modelPosition">
      <el-input v-model="dataForm.modelPosition" placeholder="所属位置"></el-input>
    </el-form-item>
    <el-form-item label="设备详情" prop="message">
      <el-input v-model="dataForm.message" placeholder="设备详情"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createDate">
      <el-input v-model="dataForm.createDate" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="是否删除  -1：已删除  0：正常" prop="delFlag">
      <el-input v-model="dataForm.delFlag" placeholder="是否删除  -1：已删除  0：正常"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          thirdPartyName: '',
          typeId: '',
          parentId: '',
          modelNumber: '',
          parentDeptId: '',
          status: '',
          modelPosition: '',
          message: '',
          createDate: '',
          delFlag: ''
        },
        dataRule: {
          thirdPartyName: [
            { required: true, message: '名称不能为空', trigger: 'blur' }
          ],
          typeId: [
            { required: true, message: '归类1.机房信息2.服务器信息3.设备安全登陆信息 4.智能管理不能为空', trigger: 'blur' }
          ],
          parentId: [
            { required: true, message: '父类型id
不能为空', trigger: 'blur' }
          ],
          modelNumber: [
            { required: true, message: '型号不能为空', trigger: 'blur' }
          ],
          parentDeptId: [
            { required: true, message: '归属机构不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态0无效1有效不能为空', trigger: 'blur' }
          ],
          modelPosition: [
            { required: true, message: '所属位置不能为空', trigger: 'blur' }
          ],
          message: [
            { required: true, message: '设备详情不能为空', trigger: 'blur' }
          ],
          createDate: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          delFlag: [
            { required: true, message: '是否删除  -1：已删除  0：正常不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/sys/subapptypeconfig/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.thirdPartyName = data.subAppTypeConfig.thirdPartyName
                this.dataForm.typeId = data.subAppTypeConfig.typeId
                this.dataForm.parentId = data.subAppTypeConfig.parentId
                this.dataForm.modelNumber = data.subAppTypeConfig.modelNumber
                this.dataForm.parentDeptId = data.subAppTypeConfig.parentDeptId
                this.dataForm.status = data.subAppTypeConfig.status
                this.dataForm.modelPosition = data.subAppTypeConfig.modelPosition
                this.dataForm.message = data.subAppTypeConfig.message
                this.dataForm.createDate = data.subAppTypeConfig.createDate
                this.dataForm.delFlag = data.subAppTypeConfig.delFlag
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sys/subapptypeconfig/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'thirdPartyName': this.dataForm.thirdPartyName,
                'typeId': this.dataForm.typeId,
                'parentId': this.dataForm.parentId,
                'modelNumber': this.dataForm.modelNumber,
                'parentDeptId': this.dataForm.parentDeptId,
                'status': this.dataForm.status,
                'modelPosition': this.dataForm.modelPosition,
                'message': this.dataForm.message,
                'createDate': this.dataForm.createDate,
                'delFlag': this.dataForm.delFlag
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
